package com.sztest.jayjun;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;
import pri.tool.pump.PumpInterface;

import android.Manifest;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.sztest.jayjun.base.BaseActivity;
import com.sztest.jayjun.bean.DebugBean;
import com.sztest.jayjun.bean.DebugItemBean;
import com.sztest.jayjun.bean.MainBean;
import com.sztest.jayjun.constant.Constant;
import com.sztest.jayjun.databinding.ActivityMainBinding;
import com.sztest.jayjun.dialog.HintDialog;
import com.sztest.jayjun.mmkv.MMKVUtils;
import com.sztest.jayjun.ui.DebugActivity;
import com.sztest.jayjun.utils.LogUtils;
import com.sztest.jayjun.utils.NumUtils;
import com.sztest.jayjun.utils.PaUtils;
import com.sztest.jayjun.utils.ToastUtils;
import com.sztest.jayjun.utils.ToneUtils;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends BaseActivity {
    private static final long PUSH_WAIT_TIME = 100;//打完针的等待时间
    private static int MODE_AUTO=0;//自动
    private static int MODE_AUTO_HALF=1;//半自动
    private static final int MODE_HAND_HALF = 2;//半手动
    private static final int MODE_HAND_NOR = 3;//全手动正常
    private static final int MODE_HAND_SLOW = 4;//全手动慢速

    private static final int TIME_HAND_NOR = 1000;//全手动正常
    private static final int TIME_HAND_SLOW = 2000;//全手动慢速


    int debugcode=1232;
    ActivityMainBinding mBinding;
    MainBean mMainBean;
    private double mZjjlDNum;
    private int mMaxCount;
    private double mOneNUm;
    private PumpInterface mBump;
    private volatile boolean longDown;
    private HintDialog mHintDialog;
    private double mfyStopTime;
    int mStep;
    private int mFyNumPa;
    private HashMap<Integer, Integer> mPaHashMap;
    private ExecutorService mWorkerThread;
    //    private AisThread mAisThread;
    boolean first=true;//是否第一次。 first=false;
    boolean isDebug=BuildConfig.DEBUG;//等2s,不等气压  adb shell input keyevent KEYCODE_F2
//    boolean isDebug=false;
    boolean isDebugShow=false;//显示调试界面
    private int mRightDebugCount;
    private int mLiftDebugCount;
    private double mSpeedThread;

    boolean mSmooth=false;//丝滑
    private long mActionUPTime;

    @Override
    protected void setDataBinding() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopCurrentData();
    }

    @Override
    protected void initView() {
        mBinding.debugView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLiftDebugCount++;
                LogUtils.e(mLiftDebugCount+"=debugView onClick="+mRightDebugCount);
                if (first&&mRightDebugCount==1&&mLiftDebugCount==2) {
                    Intent intent = new Intent(mContext, DebugActivity.class);
                    intent.putExtra(Constant.modeType,modeType);
                    startActivityForResult(intent,debugcode);
                }
            }
        });
        mBinding.debugViewRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRightDebugCount++;
            }
        });
        setfydj();//负压等级
        setzrms();//注入模式
        settjsd();//推进速度
        setZjjl();//针剂剂量
        setdjjl();//单针剂量
        setzscs();//注射次数

        fyzt();//负压状态
        setMode();
        mBinding.ivVolume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                boolean selected = !v.isSelected();
                v.setSelected(selected);
                MMKVUtils.getInstance().setBoolean(Constant.BUZZER,selected);
                LogUtils.e("ivVolume");
            }
        });
        boolean selected = MMKVUtils.getInstance().getBoolean(Constant.BUZZER);
        mBinding.ivVolume.setSelected(selected);
        ActivityCompat.requestPermissions(this,permissions,110);
        mMainBean = new MainBean();
        refreshMainBeanData();
//        allDis();
        mWorkerThread= Executors.newSingleThreadExecutor();
        MyApplication.getInstance().setWorkerThread(mWorkerThread);
        mPaHashMap = PaUtils.getPa();
//        int pa = getPa();
//        LogUtils.e("pa="+pa);
        if (isDebugShow) {
            mBinding.btnDown.setVisibility(View.VISIBLE);
            mBinding.btnUp.setVisibility(View.VISIBLE);
            mBinding.btnDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onKeyDown(Constant.FOOT,null);
                }
            });

            mBinding.btnUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onKeyUp(Constant.FOOT,null);
                }
            });
        }
        MyApplication.getInstance().OnOffBuzzer(0);
    }

    void allDis(){
//        mBinding.ivLevAdd.setEnabled(false);
//        mBinding.ivLevDown.setEnabled(false);

        mBinding.ivZjjlRight.setEnabled(false);
        mBinding.ivZjjlLeft.setEnabled(false);

        mBinding.ivZrmsRight.setEnabled(false);
        mBinding.ivZrmsLeft.setEnabled(false);

        mBinding.ivZscsRight.setEnabled(false);
        mBinding.ivZscsLeft.setEnabled(false);

        mBinding.ivTjsdRight.setEnabled(false);
        mBinding.ivTjsdLeft.setEnabled(false);

        mBinding.ivDjjlRight.setEnabled(false);
        mBinding.ivDjjlLeft.setEnabled(false);

        mBinding.tvPushDown.setEnabled(false);
        mBinding.tvPushUp.setEnabled(false);

        mBinding.debugView.setEnabled(false);
        mBinding.ivFyzt.setEnabled(false);
    }

    String[] permissions =new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};

    void refreshMainBeanData(){

        setfydjData();
        setfydjDataState();

        setzrmsData();
        setzrmsDataState();

        settjsdData();
        settjsdDataState();

        getZjjlData();
        setZjjlData();
        setZjjlDataState();

        setzscsData();
        setzscsDataState();

        setdjjlData();
        setdjjlDataState();

        checkZrms();//全手动时不选择速度之类的

        mBinding.tvPushDown.setEnabled(true);
        mBinding.tvPushUp.setEnabled(true);

        mBinding.debugView.setEnabled(true);
        mBinding.ivFyzt.setEnabled(true);
        checkCurrentIsMax();
    }

    private void checkZrms() {
        if (mMainBean.zrmsType==MODE_HAND_NOR||mMainBean.zrmsType==MODE_HAND_SLOW) {//全手动
            //推进速度
            mBinding.tvTjsd.setText("--");
            mBinding.ivTjsdRight.setEnabled(false);
            mBinding.ivTjsdLeft.setEnabled(false);

            //单针剂量
            mBinding.tvDjjl.setText("--");
            mBinding.ivDjjlRight.setEnabled(false);
            mBinding.ivDjjlLeft.setEnabled(false);

            //注射次数
            mBinding.ivZscsRight.setEnabled(false);
            mBinding.ivZscsLeft.setEnabled(false);
            mBinding.numView.setMaxNum(-1);

            mZjjlDNum = zjjlD[mMainBean.zjjlNum];
            mMaxCount = 100;//全自动打针次数
            mOneNUm=mZjjlDNum/ mMaxCount;
            mStep= (int) (zjjlDmm[mMainBean.zjjlNum]*10000/mMaxCount);
            LogUtils.e(mZjjlDNum+"=checkZrms=maxcount="+mMaxCount+"==mOneNUm=="+mOneNUm+"=mStep="+mStep);
        }
    }

    private int modeType = 1;

    void setModeNoSelect(){
        modeType = -1;
        setSelectMode(modeType);
    }

    private void setMode() {
        mBinding.ivM1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                mCurrentCount=0;
                modeType = 1;
                setSelectMode(modeType);
                mMainBean= MMKVUtils.getInstance().getMainBean(modeType);
                refreshMainBeanData();
                ToneUtils.toneMusic();
            }
        });
        mBinding.ivM2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                mCurrentCount=0;
                modeType = 2;
                setSelectMode(modeType);
                mMainBean= MMKVUtils.getInstance().getMainBean(modeType);
                refreshMainBeanData();
                ToneUtils.toneMusic();
            }
        });
        mBinding.ivM3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                mCurrentCount=0;
                modeType = 3;
                setSelectMode(modeType);
                mMainBean= MMKVUtils.getInstance().getMainBean(modeType);
                refreshMainBeanData();
                ToneUtils.toneMusic();
            }
        });
        mBinding.ivM4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                mCurrentCount=0;
                modeType = 4;
                setSelectMode(modeType);
                mMainBean= MMKVUtils.getInstance().getMainBean(modeType);
                refreshMainBeanData();
                ToneUtils.toneMusic();
            }
        });
        mBinding.ivM5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                mCurrentCount=0;
                modeType = 5;
                setSelectMode(modeType);
                mMainBean= MMKVUtils.getInstance().getMainBean(modeType);
                refreshMainBeanData();
                ToneUtils.toneMusic();
//                HintDialog hintDialog = new HintDialog(mContext);
//                hintDialog.show();
//                hintDialog.setkey();

            }
        });

        mBinding.ivM1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {LogUtils.e("onLongClick");first=false;mCurrentCount=0;setzscsData();
//                if (modeType == 1) {
                    mMainBean.modeType=1;
                    modeType = 1;
                    setCheckMode(1);
                    resetMode();
                    MMKVUtils.getInstance().setMainBean(mMainBean);
                    ToneUtils.toneMusic600();
//                }
                return true;
            }
        });
        mBinding.ivM2.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {LogUtils.e("onLongClick");first=false;mCurrentCount=0;setzscsData();
//                if (modeType == 2) {
                    mMainBean.modeType=2;
                    modeType = 2;
                    setCheckMode(2);
                    resetMode();
                    MMKVUtils.getInstance().setMainBean(mMainBean);
                    ToneUtils.toneMusic600();
//                }
                return true;
            }
        });
        mBinding.ivM3.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {LogUtils.e("onLongClick");first=false;mCurrentCount=0;setzscsData();
//                if (modeType == 3) {
                    mMainBean.modeType=3;
                    modeType = 3;
                    setCheckMode(3);
                    resetMode();
                    MMKVUtils.getInstance().setMainBean(mMainBean);
                    ToneUtils.toneMusic600();
//                }
                return true;
            }
        });
        mBinding.ivM4.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {LogUtils.e("onLongClick");first=false;mCurrentCount=0;setzscsData();
//                if (modeType == 4) {
                    mMainBean.modeType=4;
                    modeType = 4;
                    setCheckMode(4);
                    resetMode();
                    MMKVUtils.getInstance().setMainBean(mMainBean);
                    ToneUtils.toneMusic600();
//                }
                return true;
            }
        });
        mBinding.ivM5.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {LogUtils.e("onLongClick");first=false;mCurrentCount=0;setzscsData();
//                if (modeType == 5) {
                    mMainBean.modeType=5;
                    modeType = 5;
                    setCheckMode(5);
                    resetMode();
                    MMKVUtils.getInstance().setMainBean(mMainBean);
                    ToneUtils.toneMusic600();
//                }
                return true;
            }
        });
    }

    public void resetMode() {
        MyApplication.getInstance().mHandler.removeCallbacks(mRun);
        MyApplication.getInstance().mHandler.postDelayed(mRun, 500);
    }

    Runnable mRun = new Runnable() {
        @Override
        public void run() {
            setSelectMode(modeType);
        }
    };

    public void setSelectModeEnable(int num,boolean enable) {

        mBinding.ivM1.setImageLevel(num == 1 ? 2 : 0);
        mBinding.ivM2.setImageLevel(num == 2 ? 2 : 0);
        mBinding.ivM3.setImageLevel(num == 3 ? 2 : 0);
        mBinding.ivM4.setImageLevel(num == 4 ? 2 : 0);
        mBinding.ivM5.setImageLevel(num == 5 ? 2 : 0);

        mBinding.ivM1.setEnabled(enable);
        mBinding.ivM2.setEnabled(enable);
        mBinding.ivM3.setEnabled(enable);
        mBinding.ivM4.setEnabled(enable);
        mBinding.ivM5.setEnabled(enable);
    }

    public void setSelectMode(int num) {

        mBinding.ivM1.setImageLevel(num == 1 ? 2 : 0);
        mBinding.ivM2.setImageLevel(num == 2 ? 2 : 0);
        mBinding.ivM3.setImageLevel(num == 3 ? 2 : 0);
        mBinding.ivM4.setImageLevel(num == 4 ? 2 : 0);
        mBinding.ivM5.setImageLevel(num == 5 ? 2 : 0);

    }

    public void setCheckMode(int num) {

        mBinding.ivM1.setImageLevel(num == 1 ? 1 : 0);
        mBinding.ivM2.setImageLevel(num == 2 ? 1 : 0);
        mBinding.ivM3.setImageLevel(num == 3 ? 1 : 0);
        mBinding.ivM4.setImageLevel(num == 4 ? 1 : 0);
        mBinding.ivM5.setImageLevel(num == 5 ? 1 : 0);

    }

    public void setPush(boolean isDown){
        mWorkerThread.submit(new Runnable() {
            @Override
            public void run() {
//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }


                //开关步进马达
                MyApplication.getInstance().OnOffMotor(1);

                //设置马达方向,即主界面推针方向
                MyApplication.getInstance().SetMotorDirect(isDown?0:1);
                //设置马达频率和步数 即开始推针
                //freq:频率值，控制速度 即主界面上的推进速度
                //step:脉冲次数, 1个脉冲大概0.0001毫米 按照这个算法：6096个脉冲移动0.6096mm
                MyApplication.getInstance().setMotorStep((int) (20*1000),1000);

                LogUtils.e(getSpeed()*1000+"= setpush="+getStep());
            }
        });
    }
//
    public void setPushOne(boolean isDown){
        mWorkerThread.submit(new Runnable() {
            @Override
            public void run() {

                //开关步进马达
                MyApplication.getInstance().OnOffMotor(1);
                    //设置马达方向,即主界面推针方向
                MyApplication.getInstance().SetMotorDirect(isDown?0:1);
                    //设置马达频率和步数 即开始推针
                    //freq:频率值，控制速度 即主界面上的推进速度
                    //step:脉冲次数, 1个脉冲大概0.0001毫米 按照这个算法：6096个脉冲移动0.6096mm
                MyApplication.getInstance().setMotorStep((int) (30*1000),609600*2);//20
                LogUtils.e("=setPushOne=");

            }
        });
    }

    public void setPushLong(boolean isDown){
        mWorkerThread.submit(new Runnable() {
            @Override
            public void run() {
//                try {
//                    Thread.sleep(2000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }

//开关步进马达
                MyApplication.getInstance().OnOffMotor(1);

                //设置马达方向,即主界面推针方向
                MyApplication.getInstance().SetMotorDirect(isDown?0:1);

                if (mSmooth){
                    MyApplication.getInstance().setMotorStep2((int) (30*1000),0xFFFFFF);//20
                    return;
                }

                while (longDown){
                    //设置马达频率和步数 即开始推针
                    //freq:频率值，控制速度 即主界面上的推进速度
                    //step:脉冲次数, 1个脉冲大概0.0001毫米 按照这个算法：6096个脉冲移动0.6096mm

//                        MyApplication.getInstance().setMotorStep((int) (30*1000),2000);//20
                        MyApplication.getInstance().setMotorStep((int) (30*1000),60960);//20
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LogUtils.e("CONTROL_MOTOR_ENA_SET   "+longDown);
                }
            }
        });
    }


//    long mPushTime=100;
//    Runnable mRunPush=new Runnable() {
//        @Override
//        public void run() {
//            setPushOne(longDown);
//            MyApplication.getInstance().mHandler.postDelayed(mRunPush,mPushTime);
//        }
//    };

    private void fyzt() {
        mBinding.tvPushDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (System.currentTimeMillis() - mActionUPTime < 50) {
//                    return;
//                }
                LogUtils.e("tvPushDown onClick");first=false;
                setPush(true);
            }
        });

        mBinding.tvPushUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (System.currentTimeMillis() - mActionUPTime < 50) {
//                    return;
//                }
                LogUtils.e("tvPushUp onClick");first=false;
//                ToneUtils.toneMusic();
                setPush(false);
            }
        });

        mBinding.tvPushDown.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {first=false;
                longDown=true;
//                setPushLong(true);
                setPushOne(true);
//                MyApplication.getInstance().mHandler.removeCallbacks(mRunPush);
//                MyApplication.getInstance().mHandler.postDelayed(mRunPush,mPushTime);
                return true;
            }
        });

        mBinding.tvPushUp.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {first=false;
//                longDown=false;
                longDown=true;
//                setPushLong(false);
                setPushOne(false);
//                MyApplication.getInstance().mHandler.removeCallbacks(mRunPush);
//                MyApplication.getInstance().mHandler.postDelayed(mRunPush,mPushTime);
                return true;
            }
        });

        mBinding.tvPushDown.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction()==MotionEvent.ACTION_UP
                        ||motionEvent.getAction()==MotionEvent.ACTION_CANCEL){
                    if (longDown) {
                        longDown=false;
                        LogUtils.e("CONTROL_MOTOR_ENA_SET 马达");
                        MyApplication.getInstance().OnOffMotor(0);
                        MyApplication.getInstance().OnOffMotor(1);
                        if (mSmooth){
                            MyApplication.getInstance().setMotorStep2((int) (30*1000),0);
                        }
                    }
                    longDown=false;
//                    MyApplication.getInstance().mHandler.removeCallbacks(mRunPush);
                    LogUtils.e("false=longDown");
                    mActionUPTime=System.currentTimeMillis();
                    return false;
                }else if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    mCurrentCount=0;
                    setzscsData();
                    ToneUtils.toneMusic();
                }
                return false;
            }
        });

        mBinding.tvPushUp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction()==MotionEvent.ACTION_UP
                        ||motionEvent.getAction()==MotionEvent.ACTION_CANCEL){
                    if (longDown) {
                        longDown=false;
                        LogUtils.e("马达");
                        MyApplication.getInstance().OnOffMotor(0);
                        MyApplication.getInstance().OnOffMotor(1);
                        if (mSmooth){
                            MyApplication.getInstance().setMotorStep2((int) (30*1000),0);
                        }
//                        MyApplication.getInstance().setMotorStep2((int) (40*1000),0);
                    }
                    longDown=false;
//                    MyApplication.getInstance().mHandler.removeCallbacks(mRunPush);
                    LogUtils.e("false=longDown");
                    mActionUPTime=System.currentTimeMillis();
                    return false;
                }else if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    mCurrentCount=0;
                    setzscsData();
                    ToneUtils.toneMusic();
                }
                return false;
            }
        });

        mBinding.ivFyzt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                v.setSelected(!v.isSelected());
                if (v.isSelected()) {//文本显示暂停

                    setCurrentData();
                }else{//文本显示开始
                    stopCurrentData();
                }

            }
        });
    }

    private volatile int  mCurrentCount = 0;

    private void setzscs() {
        mBinding.ivZscsLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zscsNumMax);
                if (mMainBean.zscsNumMax > 0) {
                    mCurrentCount=0;
                    mMainBean.zscsNumMax --;
                    setzscsData();
                    setdjjlDataState();
                    setdjjlData();
                }

                if (mMainBean.zscsNumMax == 0) {
                    mBinding.ivZscsLeft.setEnabled(false);
                }
                mBinding.ivZscsRight.setEnabled(true);
            }
        });

        mBinding.ivZscsRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zscsNumMax);
                if (mMainBean.zscsNumMax < zscsD.length-1) {
                    mCurrentCount=0;
                    mMainBean.zscsNumMax ++;
                    setzscsData();
                    setdjjlDataState();
                    setdjjlData();
                }

                if (mMainBean.zscsNumMax == zscsD.length-1) {
                    mBinding.ivZscsRight.setEnabled(false);
                }
                mBinding.ivZscsLeft.setEnabled(true);
            }
        });
    }

    private void setzscsData() {
        mBinding.numView.setCurNum(mCurrentCount);
        if (mMainBean.zrmsType<MODE_HAND_NOR) {
            mBinding.numView.setMaxNum(zscsD[mMainBean.zscsNumMax]);
        }
        checkCurrentIsMax();
    }

    private void checkCurrentIsMax() {
        int maxNum = mBinding.numView.getMaxNum();
        LogUtils.e(mCurrentCount+"=getMaxNum="+ maxNum);
        if (maxNum!=-1&&(mCurrentCount>= maxNum)){
            mBinding.ivFyzt.setEnabled(false);
            mBinding.ivFyzt.setBackgroundResource(R.mipmap.icon_fy_start_pree);
        }else{
            LogUtils.e("修改负压状态"+mBinding.ivFyzt.isEnabled());
            if (!mBinding.ivFyzt.isEnabled()){
                mBinding.ivFyzt.setEnabled(true);
                mBinding.ivFyzt.setBackgroundResource(R.drawable.selector_main_fyzt);
            }
        }
    }

    int[] zscsD=new int[]{10,20,30,40,50,60,70,80,90,100,110,120,130,140};
    private void setzscsDataState() {
        mBinding.ivZscsRight.setEnabled(true);
        mBinding.ivZscsLeft.setEnabled(true);

        if (mMainBean.zscsNumMax == zscsD.length-1) {
            mBinding.ivZscsRight.setEnabled(false);
        }
        if (mMainBean.zscsNumMax == 0) {
            mBinding.ivZscsLeft.setEnabled(false);
        }
    }

//    String[] djjlStr = new String[]{"0.01", "0.02", "0.03", "0.04", "0.05"};

    private void setdjjl() {
        mBinding.ivDjjlLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zscsNumMax);
                if (mMainBean.zscsNumMax > 0) {
                    mCurrentCount=0;
                    mMainBean.zscsNumMax--;
                    setzscsData();
                    setzscsDataState();
                    setdjjlDataState();
                    setdjjlData();
                }

                if (mMainBean.zscsNumMax == 0) {
                    mBinding.ivDjjlLeft.setEnabled(false);
                }
                mBinding.ivDjjlRight.setEnabled(true);
            }
        });

        mBinding.ivDjjlRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zscsNumMax);
                if (mMainBean.zscsNumMax < zscsD.length - 1) {
                    mCurrentCount=0;
                    mMainBean.zscsNumMax++;
                    setzscsData();
                    setzscsDataState();
                    setdjjlDataState();
                    setdjjlData();
                }

                if (mMainBean.zscsNumMax == zscsD.length - 1) {
                    mBinding.ivDjjlRight.setEnabled(false);
                }
                mBinding.ivDjjlLeft.setEnabled(true);
            }
        });
    }

    private void setdjjlData() {// TODO: 2022/7/5 计算

        mZjjlDNum = zjjlD[mMainBean.zjjlNum];
        mMaxCount = mBinding.numView.getMaxNum();
        mOneNUm=mZjjlDNum/ mMaxCount;

        mStep= (int) (zjjlDmm[mMainBean.zjjlNum]*10000/mMaxCount);

        LogUtils.e(mZjjlDNum+"==maxcount="+mMaxCount+"==mOneNUm=="+mOneNUm+"=mStep="+mStep);
        if (mMaxCount==-1) {
            mBinding.tvDjjl.setText("--");
        }else {
            mBinding.tvDjjl.setText(NumUtils.getOneDecimal4(mOneNUm)+"");
        }
    }


    private void setdjjlDataState() {
        if (mBinding.numView.getMaxNum()==-1) {
            mBinding.ivDjjlRight.setEnabled(false);
            mBinding.ivDjjlLeft.setEnabled(false);
            return;
        }

        if (mMainBean.zscsNumMax == 0) {
            mBinding.ivDjjlLeft.setEnabled(false);
        }
        if (mMainBean.zscsNumMax == zscsD.length - 1) {
            mBinding.ivDjjlRight.setEnabled(false);
        }
    }

    String[] zjjlStr = new String[]{"0.85/1.0", "1.0/1.0", "1.5/2.0", "2.0/2.0", "1.5/3.0", "2.0/3.0", "2.5/3.0", "3.0/3.0", "4.0/5.0", "5.0/5.0"};
    Double[] zjjlD=new Double[]{0.85D,          1.0D,       1.5D,      2.0D,      1.5D,      2.0D,      2.5D,      3.0D,      4.0D,      5.0D};
    Double[] zjjlDmm=new Double[]{26.77D,      31.58D,      34.05D,    45.4D,     34.05D,    45.4D,     56.75D,    68.1D,     32.4D,     40.58D};

    String[] zjjlStr1 = new String[]{"0.85/1.0","1.0/1.0",};
    Double[]zjjlD1=new Double[]{0.85D,1.0D, };
    Double[]zjjlDmm1=new Double[]{26.77D,31.58D,};

    String[] zjjlStr2 = new String[]{"1.5/2.0", "2.0/2.0", };
    Double[]zjjlD2=new Double[]{1.5D,      2.0D,  };
    Double[]zjjlDmm2=new Double[]{34.05D,    45.4D, };


    String[] zjjlStr3 = new String[]{"1.5/3.0", "2.0/3.0", "2.5/3.0", "3.0/3.0",};
    Double[]zjjlD3=new Double[]{1.5D,      2.0D,      2.5D,      3.0D,  };
    Double[]zjjlDmm3=new Double[]{34.05D,    45.4D,     56.75D,    68.1D, };


    String[] zjjlStr5 = new String[]{"4.0/5.0", "5.0/5.0"};
    Double[]zjjlD5=new Double[]{4.0D,      5.0D};
    Double[]zjjlDmm5=new Double[]{32.4D,     40.58D};

    public void getZjjlData(){
        DebugBean debugBean = new DebugBean();
        debugBean.modeType=modeType;
        MMKVUtils.getInstance().getDebugBean(debugBean);
        if (debugBean.push==1) {
            zjjlStr=zjjlStr1;
            zjjlD=zjjlD1;
            zjjlDmm=zjjlDmm1;
        }else if (debugBean.push==2) {
            zjjlStr=zjjlStr2;
            zjjlD=zjjlD2;
            zjjlDmm=zjjlDmm2;
        }else if (debugBean.push==3) {
            zjjlStr=zjjlStr3;
            zjjlD=zjjlD3;
            zjjlDmm=zjjlDmm3;
        }else if (debugBean.push==4) {
            zjjlStr=zjjlStr5;
            zjjlD=zjjlD5;
            zjjlDmm=zjjlDmm5;
        }
    }

    private void setZjjl() {
        mBinding.ivZjjlLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zjjlNum);
                if (mMainBean.zjjlNum > 0) {
                    mMainBean.zjjlNum--;
                    setZjjlData();

                    setdjjlDataState();
                    setdjjlData();
                }

                if (mMainBean.zjjlNum == 0) {
                    mBinding.ivZjjlLeft.setEnabled(false);
                }
                mBinding.ivZjjlRight.setEnabled(true);
                mCurrentCount=0;
                setzscsData();
            }
        });

        mBinding.ivZjjlRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("ivZjjlRight onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zjjlNum);
                if (mMainBean.zjjlNum < zjjlStr.length - 1) {
                    mMainBean.zjjlNum++;
                    setZjjlData();

                    setdjjlDataState();
                    setdjjlData();
                }

                if (mMainBean.zjjlNum == zjjlStr.length - 1) {
                    mBinding.ivZjjlRight.setEnabled(false);
                }
                mBinding.ivZjjlLeft.setEnabled(true);
                mCurrentCount=0;
                setzscsData();
            }
        });
    }

    private void setZjjlData() {
        mBinding.tvZjjl.setText(zjjlStr[mMainBean.zjjlNum]);
    }


    private void setZjjlDataState() {
        mBinding.ivZjjlRight.setEnabled(true);
        mBinding.ivZjjlLeft.setEnabled(true);
        if (mMainBean.zjjlNum == 0) {
            mBinding.ivZjjlLeft.setEnabled(false);
        }
        if (mMainBean.zjjlNum == zjjlStr.length - 1) {
            mBinding.ivZjjlRight.setEnabled(false);
        }
    }

    String[] tjsdtr = new String[]{"慢速", "正常", "快速"};
    public void relatedFydj(){
        if (mMainBean.tjsdType==0) {
            mMainBean.fydjLev=6;
        }else if (mMainBean.tjsdType==1) {
            mMainBean.fydjLev=4;
        }else if (mMainBean.tjsdType==2) {
            mMainBean.fydjLev=2;
        }
        setfydjData();
        setfydjDataState();
    }

    private void settjsd() {
        mBinding.ivTjsdLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.tjsdType);
                if (mMainBean.tjsdType > 0) {
                    mMainBean.tjsdType--;
                    settjsdData();
                }

                if (mMainBean.tjsdType == 0) {
                    mBinding.ivTjsdLeft.setEnabled(false);
                }
                mBinding.ivTjsdRight.setEnabled(true);
                relatedFydj();
                mCurrentCount=0;
                setzscsData();
            }
        });

        mBinding.ivTjsdRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.tjsdType);
                if (mMainBean.tjsdType < tjsdtr.length - 1) {
                    mMainBean.tjsdType++;
                    settjsdData();
                }

                if (mMainBean.tjsdType == tjsdtr.length - 1) {
                    mBinding.ivTjsdRight.setEnabled(false);
                }
                mBinding.ivTjsdLeft.setEnabled(true);
                relatedFydj();
                mCurrentCount=0;
                setzscsData();
            }
        });

    }

    private void settjsdData() {
        mBinding.tvTjsd.setText(tjsdtr[mMainBean.tjsdType]);
    }


    private void settjsdDataState() {
        mBinding.ivTjsdRight.setEnabled(true);
        mBinding.ivTjsdLeft.setEnabled(true);
        if (mMainBean.tjsdType == 0) {
            mBinding.ivTjsdLeft.setEnabled(false);
        }
        if (mMainBean.tjsdType == tjsdtr.length - 1) {
            mBinding.ivTjsdRight.setEnabled(false);
        }
    }

    String[] zrmsStr = new String[]{"自动", "半自动", "半手动", "全手动正常","全手动慢速"};

    private void setzrms() {
        mBinding.ivZrmsLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zrmsType);
                if (mMainBean.zrmsType > 0) {
                    mMainBean.zrmsType--;
                    setzrmsData();
                }

                if (mMainBean.zrmsType == 0) {
                    mBinding.ivZrmsLeft.setEnabled(false);
                }
                mBinding.ivZrmsRight.setEnabled(true);
                refreshMainBeanData();
                checkZrms();

                mCurrentCount=0;
                setzscsData();
            }
        });

        mBinding.ivZrmsRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("onClick");first=false;
                ToneUtils.toneMusic();
                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.zrmsType);
                if (mMainBean.zrmsType < zrmsStr.length - 1) {
                    mMainBean.zrmsType++;
                    setzrmsData();
                }

                if (mMainBean.zrmsType == zrmsStr.length - 1) {
                    mBinding.ivZrmsRight.setEnabled(false);
                }
                mBinding.ivZrmsLeft.setEnabled(true);
                checkZrms();

                mCurrentCount=0;
                setzscsData();
            }
        });
    }

    private void setzrmsData() {
        mBinding.tvZrms.setText(zrmsStr[mMainBean.zrmsType]);
    }

    private void setzrmsDataState() {
        mBinding.ivZrmsRight.setEnabled(true);
        mBinding.ivZrmsLeft.setEnabled(true);
        if (mMainBean.zrmsType == 0) {
            mBinding.ivZrmsLeft.setEnabled(false);
        }
        if (mMainBean.zrmsType == zrmsStr.length - 1) {
            mBinding.ivZrmsRight.setEnabled(false);
        }

    }

    private void setfydj() {
        mBinding.ivLevAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("ivLevAdd onClick");first=false;
                LogUtils.e("=== " + mMainBean.fydjLev);
                ToneUtils.toneMusic();
//                setModeNoSelect();
                if (mMainBean.fydjLev < 10) {
                    mMainBean.fydjLev++;
                    setfydjData();
                }
                if (mMainBean.fydjLev == 10) {
                    mBinding.ivLevAdd.setEnabled(false);
                }
                mBinding.ivLevDown.setEnabled(true);
            }
        });

        mBinding.ivLevDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {LogUtils.e("ivLevDown onClick");first=false;
                ToneUtils.toneMusic();
//                setModeNoSelect();
                LogUtils.e("=== " + mMainBean.fydjLev);
                if (mMainBean.fydjLev > 1) {
                    mMainBean.fydjLev--;
                    setfydjData();
                }

                if (mMainBean.fydjLev == 1) {
                    mBinding.ivLevDown.setEnabled(false);
                }
                mBinding.ivLevAdd.setEnabled(true);
            }
        });

    }

    private void setfydjData() {
        mBinding.ivLev.setImageLevel(mMainBean.fydjLev);
        mBinding.tvLevel.setText(mMainBean.fydjLev + "");
        getCurrentFy();
    }

    private void setfydjDataState() {
        mBinding.ivLevDown.setEnabled(true);
        mBinding.ivLevAdd.setEnabled(true);
        if (mMainBean.fydjLev == 10) {
            mBinding.ivLevAdd.setEnabled(false);
        }
        if (mMainBean.fydjLev == 1) {
            mBinding.ivLevDown.setEnabled(false);
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==debugcode&&resultCode==2244) {
            mMainBean= MMKVUtils.getInstance().getMainBean(modeType);
            push2DefaultNum();
//            mMainBean.zjjlNum;
            LogUtils.e("onActivityResult="+mMainBean.fydjLev);
            refreshMainBeanData();
        }
    }

    void push2DefaultNum(){
        int push = MMKVUtils.getInstance().getPush();
        if (mMainBean!=null) {
            if (push==1) {
                mMainBean.zjjlNum=zjjlStr1.length-1;
            }else if (push==2) {
                mMainBean.zjjlNum=zjjlStr2.length-1;
            }else if (push==3) {
                mMainBean.zjjlNum=zjjlStr3.length-1;
            }else if (push==4) {
                mMainBean.zjjlNum=zjjlStr5.length-1;
            }
        }
    }

    public void stopCurrentData(){
        MyApplication.getInstance().OnOffAir(1);//4，点击暂停按键就要执行放气操作。
        MyApplication.getInstance().OnOffPump(0);
//        MyApplication.getInstance().OnOffMotor(0);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPumpComplete=false;
                mBinding.ivFyzt.setSelected(false);
                setSelectModeEnable(modeType,true);
                refreshMainBeanData();
                stopAllThread();
                MyApplication.getInstance().mHandler.removeCallbacks(mFootOneRun);
                mFooTOne=false;
                mFooTDown=false;
            }
        });
    }

    public void stopAllThread(){
        mGetAirRunningFlag=false;
        if (mPumpThread!=null) {
            mPumpThread.setExit(true);
            mPumpThread.interrupt();
            mPumpThread=null;
        }
//        if (mAisThread!=null) {
//            mAisThread.setExit(true);
//            mAisThread.interrupt();
//            mAisThread=null;
//        }
    }

    /**
     *
     mBinding.tvPushDown.setEnabled(false);
     mBinding.tvPushUp.setEnabled(false);  推针状态未考虑
     *
     */
    public void setCurrentData(){// TODO: 2022/7/8 设置打印各种参数
        allDis();
        mFooTOne=false;
        mFooTDown=false;
        mBinding.ivFyzt.setEnabled(true);
        setSelectModeEnable(-1,false);

        int i=mMainBean.fydjLev;
        getCurrentFy();

        double speed = getSpeed();

        DebugItemBean debugItemBean2 = new DebugItemBean();
        debugItemBean2.setType(2);
        debugItemBean2.setPosition(i+"");
        debugItemBean2.setNum(DebugActivity.fyTime(i));
        debugItemBean2.setModeType(modeType);
        debugItemBean2=MMKVUtils.getInstance().getDebugItemBean(debugItemBean2);
        mfyStopTime=debugItemBean2.getNum();


        LogUtils.e("负压等级="+mMainBean.fydjLev+
                "=对应负压="+mFyNumPa+
                "=注入模式="+zrmsStr[mMainBean.zrmsType]+
                "=针剂剂量="+zjjlD[mMainBean.zjjlNum]+
                "=注射次数最大="+zscsD[mMainBean.zscsNumMax]+
                "=当前注射次数="+mCurrentCount+
                "=每次注射ml="+mOneNUm+
                "=每次脉冲mm="+mStep+
                "=注射频率="+((int)(getSpeed()*1000))+
                "=推进速度="+speed+
                "=推进速度当前是="+tjsdtr[mMainBean.tjsdType]+
                "=负压暂停时间="+mfyStopTime+
                "=="+"=="+
                "=="+"=="+"=="
        )
        ;

//        if (true) {
//            new Thread(){
//                @Override
//                public void run() {
////                    while (true){
////                        setAirTest();
////                    }
//                    setAirTest();
//                }
//            }.start();
//
//            return;
//        }

//        mFooTDown=true;
        mGetAirRunningFlag=true;
//        mAisThread= new AisThread();
//        mAisThread.start();
        mPumpThread=new PumpThread();
        mPumpThread.start();

    }

    private void getCurrentFy() {
        int i=mMainBean.fydjLev;
        DebugItemBean debugItemBean = new DebugItemBean();
        debugItemBean.setType(1);
        debugItemBean.setPosition(i +"");
        debugItemBean.setNum(60.0d+ i *2);
        debugItemBean.setModeType(modeType);
        debugItemBean=MMKVUtils.getInstance().getDebugItemBean(debugItemBean);
        mFyNumPa = (int) debugItemBean.getNum();
        LogUtils.e("fynumpa="+mFyNumPa);
    }

    private double getSpeed() {
        DebugBean debugBean = new DebugBean();
        debugBean.modeType=modeType;
        debugBean=MMKVUtils.getInstance().getDebugBean(debugBean);
        double speed=debugBean.speed_nor;
        if (mMainBean.tjsdType==0) {
            speed=debugBean.speed_slow;
        }else if (mMainBean.tjsdType==1) {
            speed=debugBean.speed_nor;
        }else if (mMainBean.tjsdType==2) {
            speed=debugBean.speed_fast;
        }
        LogUtils.w("speed="+speed);
        return speed;
    }

    /**
     * 单针注射距离
     * @return
     */
    private int getStep(){
        return mStep;
    }

   volatile boolean mPumpComplete=false;//是否达到负压
    volatile  boolean mFooTDown=false;//脚踏是否按着
    volatile  boolean mFooTOne=false;//单踩一次
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.e("onKeyDown==="+keyCode);
        if (keydownFlag){
            return true;
        }
        keydownFlag = true;
        if (keyCode== Constant.FOOT) {
            LogUtils.e("f3===");
            if (mMainBean.zrmsType == MODE_AUTO_HALF) {//半自动，连续踩
               mFooTDown=true;
            } else if (mMainBean.zrmsType==MODE_HAND_HALF) {//半手动，单踩
                mFooTOne=true;
            }else if (mMainBean.zrmsType == MODE_HAND_NOR||mMainBean.zrmsType == MODE_HAND_SLOW){//全手动。
                mFooTDown=true;
            }
            return true;
        }else if (keyCode==Constant.ALARM ) {
//            allDis();
            return true;
        }else if (keyCode== Constant.CLOSESYS) {
            LogUtils.e("131???");
            MyApplication.getInstance().mHandler.postDelayed(mCloseSys,200);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    Runnable mFootOneRun=new Runnable() {
        @Override
        public void run() {
            mFooTOne=true;
        }
    };

    // TODO: 2022/7/5 按键
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        LogUtils.e("onKeyUp==="+keyCode);
        keydownFlag = false;
        if (keyCode== Constant.FOOT) {
            mFooTDown=false;
            LogUtils.e("f3===");
            if (mMainBean.zrmsType == MODE_AUTO_HALF) {//半自动，连续踩

            } else if (mMainBean.zrmsType==MODE_HAND_HALF) {//半手动
//                if (mPumpComplete){
//                    mFooTOne=true;
//                }
            } /*else if (mMainBean.zrmsType>=MODE_HAND_HALF) {//有手动的时候，单踩
                if (mPumpComplete){
                    if (mMainBean.zrmsType==MODE_HAND_NOR){
                        MyApplication.getInstance().mHandler.removeCallbacks(mFootOneRun);
                        MyApplication.getInstance().mHandler.postDelayed(mFootOneRun,TIME_HAND_NOR);
                    }else if (mMainBean.zrmsType==MODE_HAND_SLOW){
                        MyApplication.getInstance().mHandler.removeCallbacks(mFootOneRun);
                        MyApplication.getInstance().mHandler.postDelayed(mFootOneRun,TIME_HAND_NOR);
                    }else{
                        mFooTOne=true;
                    }
                }
            }*/
            return true;
        }else if (keyCode==Constant.ALARM ) {
            if (mHintDialog == null) {
                mHintDialog= new HintDialog(mContext);
            }
            mHintDialog.show();
            mHintDialog.setkey();
            stopCurrentData();
            mCurrentCount=0;
            mBinding.numView.setCurNum(mCurrentCount);
            return true;
        }else if (keyCode==Constant.CLOSESYS ) {
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }


    /**
     *
     * @return 负压是否正常
     */
    public boolean checkfyisError(){
//        int curAirPressure = getPa();
//        return !(Math.abs(curAirPressure - TargetAirPressure) < (TargetAirPressure/10));
        return false;
    }

    /**
     *
     * @return 负压是否已经下降
     */
    public boolean checkfyisErrorDown(){
        double mPa = getPa();
        if (mPa<5){
            return false;
        }else {
            return true;
        }
//        return false;
    }

    public void showErrorDialog(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mHintDialog == null) {
                    mHintDialog =new HintDialog(mContext);
                }
                mHintDialog.show();
                mHintDialog.setfy();
                stopCurrentData();
            }
        });
    }

    public void setmotor(){
        addCurrentCount();
        MyApplication.getInstance().OnOffMotor(1);
        MyApplication.getInstance().SetMotorDirect(1);
        MyApplication.getInstance().setMotorStep((int) (mSpeedThread*1000),getStep());
        LogUtils.e(getSpeed()*1000+"=脉冲="+getStep());
    }

    public void addCurrentCount(){
        mCurrentCount++;
        if (ToastUtils.isMainThread()){
            mBinding.numView.setCurNum(mCurrentCount);
        }else{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBinding.numView.setCurNum(mCurrentCount);
                }
            });
        }
        LogUtils.e("当前针数="+mCurrentCount);
    }

    long waitTime(double freq, int step){
        long time= (long) (1/(freq*1000)*step);
        return time;
    }

    PumpThread mPumpThread;// TODO: 2022/7/9 0009 调负压
    public class PumpThread extends Thread {

        final String TAG="PumpThread";

        volatile  int mCloseTime=5000;//millis

        volatile  int mStartTime=5000;
        volatile  int mTime=0;

        volatile boolean exit;

        public boolean continuMotro(){
            return  mMainBean.zrmsType == MODE_AUTO||(mFooTDown&&(mMainBean.zrmsType == MODE_AUTO_HALF));
        }


        @Override
        public void run() {
            //设置马达频率和步数
            //freq:频率值，控制速度 即主界面上的推进速度
            //step:脉冲次数, 1个脉冲大概0.0001毫米 按照这个算法：6096个脉冲移动0.6096mm
            mSpeedThread = 0;
            if (mMainBean.zrmsType == MODE_HAND_NOR){
                DebugBean debugBean = new DebugBean();
                debugBean.modeType=modeType;
                debugBean=MMKVUtils.getInstance().getDebugBean(debugBean);
                mSpeedThread =debugBean.speed_nor;
                LogUtils.w("手动 正常 speed="+ mSpeedThread);
            }else if (mMainBean.zrmsType == MODE_HAND_SLOW){
                DebugBean debugBean = new DebugBean();
                debugBean.modeType=modeType;
                debugBean=MMKVUtils.getInstance().getDebugBean(debugBean);
                mSpeedThread =debugBean.speed_slow;
                LogUtils.w("手动 慢速speed="+ mSpeedThread);
            }else{
                mSpeedThread =getSpeed();
            }
//            MyApplication.getInstance().OnOffMotor(1);
//            MyApplication.getInstance().SetMotorDirect(1);
            //设置马达方向,即主界面推针方向
            while (!exit){
                mPumpComplete=false;//调试使用
                //开关空气泵,常开,产生负压
                MyApplication.getInstance().OnOffPump(1);
                MyApplication.getInstance().OnOffAir(0);

                try {
                    if (isDebug) {
                        Thread.sleep(2000);
                    }else {
                        double air = setAir();
                    }
                } catch (InterruptedException e) {
                    MyApplication.getInstance().OnOffAir(0);
                    e.printStackTrace();
                    break;
                }

                MyApplication.getInstance().OnOffPump(0);
//                LogUtils.d(TAG,exit+"==air=="+air+"==mCloseTime="+mCloseTime+"==mStartTime=="+mStartTime);
                LogUtils.d(TAG,mPumpComplete+"==air=="+mTime+"=Pa="+mFyNumPa);
                //预想此处已经满足负压
                if (!mPumpComplete){
                    mPumpComplete=true;
                    while (!exit){
                        if (continuMotro()){
                            setmotor();
                            try {
                                sleep(PUSH_WAIT_TIME);//等待针打完
//                                sleep(PUSH_WAIT_TIME+waitTime(mSpeedThread,getStep()));//等待针打完
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                if (exit) {
                                    return;
                                }
                            }
                            //检查负压是否异常
                            if (checkfyisError()){//异常
                                if (exit) {
                                    return;
                                }
                                showErrorDialog();
                                return;
                            }
                            LogUtils.e("OnOffAir 0");
                            MyApplication.getInstance().OnOffAir(1);//  mMainBean.zrmsType == MODE_HAND_NOR||mMainBean.zrmsType == MODE_HAND_SLOW
                            if (mMainBean.zrmsType == MODE_HAND_NOR){
                                try {
                                    sleep(TIME_HAND_NOR);
                                } catch (InterruptedException e) {
                                    if (exit) {
                                        return;
                                    }
                                    e.printStackTrace();
                                }
                                LogUtils.e("TIME_HAND_NOR="+TIME_HAND_NOR);
                            }else if (mMainBean.zrmsType == MODE_HAND_SLOW){
                                try {
                                    sleep(TIME_HAND_SLOW);
                                } catch (InterruptedException e) {
                                    if (exit) {
                                        return;
                                    }
                                    e.printStackTrace();
                                }
                                LogUtils.e("TIME_HAND_SLOW="+TIME_HAND_SLOW);
                            }else{
                                LogUtils.e("mfyStopTime="+mfyStopTime);
                                try {
                                    sleep((long) (mfyStopTime*1000));
                                } catch (InterruptedException e) {
                                    if (exit) {
                                        return;
                                    }
                                    e.printStackTrace();
                                }
                            }


                            //检查负压是否异常
                            if (checkfyisErrorDown()){//异常 负压是否下降
                                if (exit) {
                                    return;
                                }
                                showErrorDialog();
                                return;
                            }
                            if (mCurrentCount==mBinding.numView.getMaxNum()){
                                stopCurrentData();
                                return;
                            }
                            mPumpComplete=false;
                            LogUtils.e("下一针");
                            break;
                        }else if (mFooTDown&&(mMainBean.zrmsType == MODE_HAND_NOR||mMainBean.zrmsType == MODE_HAND_SLOW)){//全手动

                            while (mFooTDown){//打针
                                MyApplication.getInstance().OnOffMotor(1);
                                MyApplication.getInstance().SetMotorDirect(1);
                                MyApplication.getInstance().setMotorStep((int) (mSpeedThread *1000),2000);
//                                try {
//                                    sleep(PUSH_WAIT_TIME+waitTime(mSpeedThread,100));//等待针打完
//                                } catch (InterruptedException e) {
//                                    e.printStackTrace();
//                                    pumpInterface.OnOffAir(0);
//                                }
                                if (exit) {
                                    return;
                                }
                            }

                            MyApplication.getInstance().OnOffAir(1);
                            try {
                                sleep((long) (mfyStopTime*1000));
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                if (exit) {
                                    return;
                                }
                            }

                            //检查负压是否异常
                            if (checkfyisErrorDown()){//异常 负压是否下降
                                if (exit) {
                                    return;
                                }
                                showErrorDialog();
                                return;
                            }
                            mPumpComplete=false;
                        }else if (mFooTOne){
                            mFooTOne=false;
                            setmotor();
                            try {
                                sleep(PUSH_WAIT_TIME);//等待针打完
//                                sleep(PUSH_WAIT_TIME+waitTime(mSpeedThread,getStep()));//等待针打完
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                if (exit) {
                                    return;
                                }
                            }
                            //检查负压是否异常
                            if (checkfyisError()){//异常
                                if (exit) {
                                    return;
                                }
                                showErrorDialog();
                                return;
                            }
                            LogUtils.e("OnOffAir 0");
                            MyApplication.getInstance().OnOffAir(1);
                            try {
                                sleep((long) (mfyStopTime*1000));
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                if (exit) {
                                    return;
                                }
                            }

                            //检查负压是否异常
                            if (checkfyisErrorDown()){//异常 负压是否下降
                                if (exit) {
                                    return;
                                }
                                showErrorDialog();
                                return;
                            }
                            if (mCurrentCount==mBinding.numView.getMaxNum()){
                                stopCurrentData();
                                return;
                            }
                            mPumpComplete=false;
                            break;
                        }
                        try {
                            sleep(10);
                            double mPa = getPa();
                            Log.v("wait ",mPa+"=wait 10");
                            if (mPa<1){
                                break;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            LogUtils.d(TAG,"==air==exit==");
        }




        public void setCloseTime(int closeTime) {
            mCloseTime = closeTime;
        }

        public void setStartTime(int startTime) {
            mStartTime = startTime;
        }

        public void setTime(int time) {
            mTime = time;
        }

        public void setExit(boolean exit) {
            this.exit = exit;
        }

    }

    volatile boolean  mGetAirRunningFlag = true;//强行停止获取气压的循环

    public double getPa(){
        double pas=0d;

        int ad = MyApplication.getInstance().get_mPa();
//        int ad = new Random().nextInt()%16777216+8388608;

        if (ad > 8388608)          //超过 8388606 为负压值，需在显示终端做处理
        {
            pas = 0-(ad - 16777216)/64d/1000; //单位为 kpa
        }
        else {
            pas = ad / 64d / 1000; //单位为 kpa
        }
        return NumUtils.getOneDecimal1(pas);
    }

    private double setAir() throws InterruptedException {
        double curAirPressure = 0;
        while(mGetAirRunningFlag) {
            //阻塞获取气压值是否达到预设值
            curAirPressure = getPa();
            if(Math.abs(curAirPressure - mFyNumPa) < (mFyNumPa/10d))
            {//传感器读数与目标数值误差10%以内， 认为OK
                break;
            }
           Thread.sleep(100);
        }
        Log.d("lwt","getAir curAirPressure:"+curAirPressure);
        return curAirPressure;
    }

    void setAirTest(){
        /*if (true) {
            double pas=0d;

//        int ad = MyApplication.getInstance().getPumpInterface().get_mPa();
            int ad = new Random().nextInt()%16777216+8388608;

            if (ad > 8388608)          //超过 8388606 为负压值，需在显示终端做处理
            {
                pas = 0-(ad - 16777216)/64d/1000; //单位为 kpa
            }
            else {
                pas = ad / 64d / 1000; //单位为 kpa
            }
            LogUtils.e("数字="+NumUtils.getOneDecimal1(pas));

            return;
        }*/



        double curAirPressure = 0;
        while(true) {
            //阻塞获取气压值是否达到预设值
            curAirPressure +=0.12;
            double abs = Math.abs(curAirPressure - mFyNumPa);
            int i = mFyNumPa / 10;
            if(abs < i)
            {//传感器读数与目标数值误差10%以内， 认为OK
                break;
            }
            Log.d("lwt",abs+"=abs=压力值="+curAirPressure+"=需要的差值="+i);
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        Log.d("lwt","getAir curAirPressure:"+curAirPressure);
    }

//    int TargetAirPressure = 20; //根据主界面设置的气压值，查表得出目标气压值对应的传感器数值
//    int mAirSwitchOnPercent = 30; //根据主界面设置， 获取阀门打开的占比
//    int mLoopDueration = 3000; //一个开关循环所用时长， 单位毫秒， 经验值，客户提供

//    public class AisThread extends Thread{
//        volatile boolean mAirSwitchRunningFlag = false;//阀门开关线程标志，需要泄压时置为false,线程开启前置为true
//        @Override
//        public void run() {
//            try{
//                while (!mAirSwitchRunningFlag)
//                {
//                    MyApplication.getInstance().getPumpInterface().OnOffAir(1);
//                    Thread.sleep(mLoopDueration*mAirSwitchOnPercent/100);//开启占比
//                    MyApplication.getInstance().getPumpInterface().OnOffAir(0);
//                    Thread.sleep(mLoopDueration*(100-mAirSwitchOnPercent)/100);//关闭占比
//                }
//                Log.d("lwt","mAirSwitchThread FINISHED!!");
//
//            }catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//        }
//        public void setExit(boolean exit) {
//            this.mAirSwitchRunningFlag = exit;
//        }
//    }

}